package com.mikhailenko.transportMVC.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.mikhailenko.transportMVC.exeption.CriteriaNotFoundExeption;
import com.mikhailenko.transportMVC.model.Criteria;
import com.mikhailenko.transportMVC.model.CriteriaFake;
import com.mikhailenko.transportMVC.model.Firm;
import com.mikhailenko.transportMVC.model.Group;
import com.mikhailenko.transportMVC.model.Term;
import com.mikhailenko.transportMVC.service.CriteriaService;
import com.mikhailenko.transportMVC.service.FirmService;
import com.mikhailenko.transportMVC.service.GroupService;
import com.mikhailenko.transportMVC.service.TermService;

@Controller
@RequestMapping(value="/criteria")
public class CriteriaController {
	
	@Autowired
	private CriteriaService criteriaService;
	
	@Autowired
	private FirmService firmServie;
	
	@Autowired
	private TermService termService;
	
	@Autowired
    private GroupService groupService;
	
	private static Logger log = Logger.getLogger(CriteriaController.class.toString());
	
	
	@RequestMapping(value="/create", method=RequestMethod.GET)
	public ModelAndView newFirmPage(){
		ModelAndView mav = new ModelAndView("criteria-new", "criteria", new CriteriaFake());
		
		List<Firm> firms = firmServie.findAll();
		mav.addObject("firms", firms);
		
		List<Term> terms = termService.findAll();
		mav.addObject("terms", terms);
		
		List<Group> groups = groupService.findAll();
		mav.addObject("groups", groups);
		
		log.info("Form creation ends");
		return mav;
		
	}
	
	@RequestMapping(value="/create", method=RequestMethod.POST)
	public ModelAndView createNewFirm(@ModelAttribute CriteriaFake criteria,
			final RedirectAttributes redirectAtributes){
	    log.info("New criteria creation begins");
	    
		ModelAndView mav = new ModelAndView();
		String message = "Company: "+criteria.getName()+" was added successfully";

		Firm firm = firmServie.findByName(criteria.getFirm());
		Term term = termService.findbyName(criteria.getTerm());
		Group group = groupService.findByName(criteria.getGroup());
		
		Criteria realCriteria = new Criteria();
		realCriteria.setName(criteria.getName());
		realCriteria.setFirm(firm);
		realCriteria.setTerm(term);
		realCriteria.setGroup(group);
		realCriteria.setStatus(criteria.getStatus());
		
		mav.setViewName("redirect:/criteria/list");
		
		criteriaService.create(realCriteria);
		
		redirectAtributes.addFlashAttribute("message", message);
		return mav;
	}
	
	   @RequestMapping(value="/list", method=RequestMethod.GET)
	    public ModelAndView firmListPage(){
	        ModelAndView mav = new ModelAndView("criteria-list");
	        List<Criteria> criteriaList = criteriaService.findAll();
	        mav.addObject("criteriaList", criteriaList);
	        return mav;
	    }
	   
	   
	   @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	    public ModelAndView editGroupPage(@PathVariable Integer id) {
	        ModelAndView mav = new ModelAndView("criteria-edit");
	        Criteria criteria = criteriaService.findById(id);
	        
	        CriteriaFake criteriaFake = new CriteriaFake();
	        criteriaFake.setId(criteria.getId());
	        criteriaFake.setName(criteria.getName());
//	        criteriaFake.setFirm(criteria.getFirm().getName());
//	        criteriaFake.setGroup(criteria.getGroup().getName());
//	        criteriaFake.setTerm(criteria.getTerm().getName());
//	        criteriaFake.setStatus(criteria.getStatus());
	        
	        List<Firm> firms = firmServie.findAll();
	        mav.addObject("firms", firms);
	        
	        List<Term> terms = termService.findAll();
	        mav.addObject("terms", terms);
	        
	        List<Group> groups = groupService.findAll();
	        mav.addObject("groups", groups);
	        
	        mav.addObject("criteria", criteriaFake);
	        return mav;
	    }

	    @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	    public ModelAndView editUser(@ModelAttribute CriteriaFake criteriaFake,
	            BindingResult result,
	            @PathVariable Integer id,
	            final RedirectAttributes redirectAttributes)
	            throws CriteriaNotFoundExeption {

	        Criteria criteria = criteriaService.findById(criteriaFake.getId());
	        Firm firm = firmServie.findByName(criteriaFake.getFirm());
	        Term term = termService.findbyName(criteriaFake.getTerm());
	        Group group = groupService.findByName(criteriaFake.getGroup());
	        
	        criteria.setName(criteriaFake.getName());
	        criteria.setFirm(firm);
	        criteria.setGroup(group);
	        criteria.setTerm(term);
	        criteria.setStatus(criteriaFake.getStatus());
	        
	        criteriaService.update(criteria);

	        ModelAndView mav = new ModelAndView("redirect:/criteria/list");
	        String message = "User:"+criteria.getName()+" was successfully updated.";

	        redirectAttributes.addFlashAttribute("message", message);
	        return mav;
	    }
	   
	    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	    public ModelAndView deleteUser(@PathVariable Integer id,
	            final RedirectAttributes redirectAttributes)
	            throws  CriteriaNotFoundExeption {

	        ModelAndView mav = new ModelAndView("redirect:/criteria/list");

	        Criteria criteria = criteriaService.delete(id);
	        String message = "The user " + criteria.getName()
	                + " was successfully deleted.";

	        redirectAttributes.addFlashAttribute("message", message);
	        return mav;
	    }
	    
}
